#include <Arduino.h>
SemaphoreHandle_t binary_semaphore = nullptr;
TickType_t timeOut = 5000;

#define interruptPin 0
void FlipLED(void* p)
{
  while(1)
  {
    if(pdTRUE == xSemaphoreTake(binary_semaphore, timeOut))
    {
      Serial.println("xSemaphoreTake succeed.");
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    }
    else
    {
      Serial.println("xSemaphoreTake failed.");      
    }
  }
}
void ARDUINO_ISR_ATTR interruptPinISR()
{
  Serial.println("Enter interruptPinISR.");
  xSemaphoreGive(binary_semaphore);
}
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(interruptPin, interruptPinISR, FALLING);
  binary_semaphore = xSemaphoreCreateBinary();
  if(binary_semaphore == nullptr)
  {
    Serial.println("xSemaphoreCreateBinary failed, system restart.");
    ESP.restart();
  }
  xTaskCreate(FlipLED, "FlipLED", 1024, nullptr, 1, nullptr);

}

void loop() {
  // put your main code here, to run repeatedly:

}
